﻿using CQIE.LabAssist.DataAccesss;
using CQIE.LabAssist.Models;
using CQIE.LabAssist.PublicHelp;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using static CQIE.LabAssist.Controllers.UserController;

namespace CQIE.LabAssist.Controllers
{
    /// <summary>
    /// 用户相关控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly _DbContext _Db;
        private readonly IUserService _userService;
        public UserController(_DbContext _DbContext, IUserService userService)
        {
            _userService = userService;
            _Db = _DbContext;
        }

        #region 登录
        public record login
        {
            public string num { get; set; }
            public string password { get; set; }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="num"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login([FromBody] login login)
        {
            return (ActionResult)new JsonResult(_userService.Login(login.num, login.password)).Value;
        }
        #endregion

        #region 菜单栏获取
        /// <summary>
        /// 菜单栏获取
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>

        [HttpGet]
        public ActionResult getMenu(int userId)
        {
            return new JsonResult(_userService.getMenu(userId));
        }
        #endregion

        #region 添加实验室管理员
        public record user
        {
            public string number { get; set; }

            public string Password { get; set; }

            public string Name { get; set; }

            public string Phone { get; set; }

            public int academician { get; set; }

            public List<string> Role { get; set; }

        }
        /// <summary>
        /// 添加实验室管理员
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]

        public IActionResult addUser([FromBody] user user)
        {
            User newuser = new User();
            newuser.acadminianId = user.academician;
            newuser.Name = user.Name;
            newuser.Phone = user.Phone;
            newuser.num = user.number;
            newuser.Password = user.Password;
            List<string> roles = user.Role;
            if (_userService.addUser(newuser,roles))
            {
                return new JsonResult("添加成功");
            }
            else
            {
                return  BadRequest("添加失败");

            }
        }

        //public IActionResult addUser(string uid, string jsonStirng)
        //{
        //        User user = Db.User.Where(u => u.SchoolNumber == uid).Single();
        //        // 反序列化JSON字符串为匿名对象
        //        var jsonUser = JsonConvert.DeserializeObject<dynamic>(jsonStirng);
        //        user.TelephoneNumber = jsonUser.tel;
        //        user.Address = jsonUser.address;
        //        Db.SaveChanges();
        //        return "Operation completed successfully.";
        //}
        #endregion

        #region 人员获取
        /// <summary>
        /// 人员获取
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public ActionResult getUser()
        {
            return new JsonResult(_userService.getUser());
        }
        #endregion

        #region 修改信息
        public record upuser
        {
            public int Id { get; set; }
            public string number { get; set; }
            public string Password { get; set; }
            public string Name { get; set; }
            public string Phone { get; set; }
            public bool State { get; set; }
            public int AcademicianId { get; set; }
            public List<string> Role { get; set; }
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult updateUser([FromBody] upuser user)
        {
            User oleuser = new User();

            oleuser.Name = user.Name;
            oleuser.Phone = user.Phone;
            oleuser.State = user.State;
            oleuser.Id = user.Id;
            oleuser.num = user.number;
            oleuser.Password = user.Password;
            oleuser.acadminianId=user.AcademicianId;
            List<string> Roles = user.Role;
            if (_userService.updateUser(oleuser, Roles))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return BadRequest("修改失败");
            }

        }
        #endregion

        #region  删除人员


        /// <summary>
        /// 删除人员
        /// </summary>

        /// <returns></returns>
        [HttpDelete]
        public IActionResult deteltUser(int UserId)
        {
            if (_userService.deleteUser(UserId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return BadRequest("删除失败");

            }

        }
        #endregion

        /// <summary>
        /// 获取全部角色的菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getRolesMenu()
        {
           
            
                return (IActionResult)new JsonResult(_userService.getRolesMenuStatistics()).Value;
          

        }


        /// <summary>
        /// 根据角色id获取角色对应的菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getRolesMenuByid(int roleId)
        {
            return (IActionResult)new JsonResult(_userService.getRolesMenuByid(roleId)).Value;
        }

        /// <summary>
        /// 获取全部的权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getAllMenu()
        {


            return (IActionResult)new JsonResult(_userService.getAllMenu()).Value;


        }
        /// <summary>
        /// 获取全部的角色
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getAllRoles()
        {
            return new JsonResult(_userService.getAllRoles());

        }


        /// <summary>
        /// 获取一级菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getOneMenu()
        {
            return new JsonResult(_userService.getOneMenu());
        }
        /// <summary>
        /// 根据角色id获取角色对应的一级菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getOneMenuByid(int roleId)
        {
            return new JsonResult(_userService.getOneMenuByid(roleId));
        }
        public record jus
        {
            public int roleid {  get; set; }
            public int menid { get; set; }
            public bool state {  get; set; }

        }
        [HttpPost]
        public IActionResult jusSetting([FromBody] jus juss)
        {
            if ( _userService.jusSetting(juss.roleid,juss.menid,juss.state))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return BadRequest("修改失败");
            }
          
        }
        public record  uprole
        {
            public int roleid { get; set; }
            public string name { get; set; }
           

        }
        [HttpPost]
        public IActionResult roleSetting([FromBody] uprole uprole)
        {
            if (_userService.roleSetting(uprole.roleid, uprole.name))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return BadRequest("修改失败");
            }
        }
        [HttpGet]
        public IActionResult menuTwo( int menid)
        {
            return new JsonResult(_userService.menuTwo(menid));
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getUserInfo(int uid)
        {
            return _userService.getUserInfo(uid);
        }
        /// <summary>
        /// 更改用户信息
        /// </summary>
        /// <param name="nuser"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<bool> changeUserInfo(User nuser)
        {
            return _userService.changeUserInfo(nuser);
        }
        /// <summary>
        /// 更改用户密码
        /// </summary>
        /// <param name="nuser"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<bool> changeUserPassword(User nuser)
        {
            return _userService.changeUserPassword(nuser);
        }
    }
}